package com.iemes.controller.finance;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AccountFormMap;
import com.iemes.mapper.finance.AccountMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/finance")
@Controller
public class AccountController extends BaseController {
	
	@Inject
	private AccountMapper accountMapper;

	
	@RequestMapping("account")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/account_list";
	}
	
	
	@ResponseBody
	@RequestMapping("account_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		AccountFormMap accountFormMap=getFormMap(AccountFormMap.class);
		
		List<Map<String,Object>> accountFormMaps=accountMapper.getAccountByCondition(accountFormMap);
		for(Map<String,Object>map:accountFormMaps){
			//订单的单价
			double oprice=Double.parseDouble(map.get("oprice").toString());
			//税率
			double taxRate=Double.parseDouble(map.get("taxRate").toString());
			
			//将单价存进对账单
			map.put("priceExcludingTax", oprice);
			//含税单价
			map.put("unitPriceTaxIncluded", (double) Math.round((1+taxRate)*oprice * 100) / 100);
			
			//数量
			int many=Integer.parseInt(map.get("actualDelivery").toString());
			
			//含税单价
			double money=Double.parseDouble(map.get("unitPriceTaxIncluded").toString());
			//含税金额
			map.put("aprice", money*many);
			
			if(Integer.parseInt(map.get("accountCheck").toString())==0){
				map.put("accountCheck", "未对账");
			}else{
				map.put("accountCheck", "已对账");
			}
		}
		LuogicPaging(pageNow, pageSize, accountFormMaps);
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping(value="account_deleteEntity")
	@SystemLog(module="对账单",methods="对账单-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		
		AccountFormMap accountFormMap=new AccountFormMap();
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			accountFormMap.put("state", 1);
			accountFormMap.put("id", str[i]);
			accountMapper.editEntity(accountFormMap);
		}
		
		return "success";
	}
	
	
	@RequestMapping("account_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		AccountFormMap accountFormMap=accountMapper.getAccountById(Integer.parseInt(id));
		model.addAttribute("accountFormMap", accountFormMap);

		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/account_edit";
		
	}
		
		
	@ResponseBody
	@RequestMapping(value="account_editEntity")
	@SystemLog(module="对账单",methods="对账单-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		AccountFormMap accountFormMap=getFormMap(AccountFormMap.class);
		accountMapper.editEntity(accountFormMap);
		return "success";
	}
	
	
}
